import dayjs from 'dayjs';

/* eslint-disable */
/**
 * 货币格式化
 * @param price
 */
function formatPrice(price: number) {
  return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

// 返回格式化金额
export function unitPrice(val: number, unit?: string, location?: string) {
  if (!val) val = 0;
  const price = formatPrice(val);
  if (location === 'before') {
    return price.substr(0, price.length - 3);
  }
  if (location === 'after') {
    return price.substr(-2);
  }
  return (unit || '') + price;
}

/**
 * 处理unix时间戳，转换为可阅读时间格式
 * @param unix
 * @param format
 * @returns {*|string}
 */
export function unixToDate(unix: number, format?: string) {
  // eslint-disable-next-line no-underscore-dangle
  let _format = format || 'yyyy-MM-dd hh:mm:ss';
  const d = new Date(unix * 1000);
  const o:any = {
    'M+': d.getMonth() + 1,
    'd+': d.getDate(),
    'h+': d.getHours(),
    'm+': d.getMinutes(),
    's+': d.getSeconds(),
    'q+': Math.floor((d.getMonth() + 3) / 3),
    'S': d.getMilliseconds(),
  };
  if (/(y+)/.test(_format))
    _format = _format.replace(
      RegExp.$1,
      `${d.getFullYear()}`.substr(4 - RegExp.$1.length)
    );
  // eslint-disable-next-line no-restricted-syntax
  for (const k in o)
    if (new RegExp(`(${k})`).test(_format)) {
      _format = _format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
      );
    }
  return _format;
}

export function unixSellerBillStatus(val: any) {
  if (val == "OUT") {
    return "待对账";
  } else if (val == "CHECK") {
    return "待结算";
  } else {
    return '已完成';
  }
}
// 处理金钱格式
export const money = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
//处理时间 00:00:00
export const dayFormatHHssMM = (day: string) => {
  return dayjs(new Date(day).getTime()).format('YYYY-MM-DD HH:mm:ss')
}

/**
 * 
 * @param obj
 * @returns {*}
 */
export  function cloneObj(obj:any) {
  let str = null
  let newobj: any = obj.constructor === Array ? [] : {}
  if (typeof obj !== 'object') {
    return
  } else if (window && window.JSON) {
    str = JSON.stringify(obj) // 系列化对象
    newobj = JSON.parse(str) // 还原
  } else {
    for (var i in obj) {
      newobj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i]
    }
  }
  return newobj
}